﻿using SuporeSliplampApp.Base;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using static SuporeSliplampApp.Models.CameraModel;
using System.Windows.Controls;

namespace SuporeSliplampApp.ViewModels
{
    public class CameraViewModel:NotifyBase
    {
        public static CameraModel camModel { get; set; } = new CameraModel();

        /*private void OnEventError()
        {
            camModel.Cam.close();
            camModel.Cam = null;
            Marshal.FreeHGlobal(camModel.Buf);
        }

        private void OnEventDisconnect()
        {
            camModel.Cam.close();
            camModel.Cam = null;
            Marshal.FreeHGlobal(camModel.Buf);
            //MessageBox.Show("Camera disconnect.");
        }

        private void UpdateExpoTime()
        {
            int val = 0;
            camModel.Cam.get(uvcham.eCMD.EXPOTIME, out val);
        }

        private void UpdateAwb()
        {
            int val = 0;
            camModel.Cam.get(uvcham.eCMD.WBRED, out val);
            camModel.Cam.get(uvcham.eCMD.WBGREEN, out val);
            camModel.Cam.get(uvcham.eCMD.WBBLUE, out val);
        }

        private void OnEventImage()
        {
            if (camModel.Bmp != null)
            {
                try
                {
                    camModel.Bmp.Lock();
                    try
                    {
                        uvcham.memcpy(camModel.Bmp.BackBuffer, camModel.Buf, new IntPtr(uvcham.TDIBWIDTHBYTES(camModel.Bmp.PixelWidth * 32) * camModel.Bmp.PixelHeight));
                        camModel.Bmp.AddDirtyRect(new Int32Rect(0, 0, camModel.Bmp.PixelWidth, camModel.Bmp.PixelHeight));
                    }
                    finally
                    {
                        camModel.Bmp.Unlock();
                    }

                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        private void startDevice(string id)
        {
            camModel.Cam = uvcham.open(id);
            if (camModel.Cam != null)
            {
                camModel.Cam.put(uvcham.eCMD.FORMAT, 1);
                int width = 0, height = 0, res = 0;
                camModel.Cam.get(uvcham.eCMD.RES, out res);
                camModel.Cam.get(uvcham.eCMD.WIDTH | ((uvcham.eCMD)res), out width);
                camModel.Cam.get(uvcham.eCMD.HEIGHT | ((uvcham.eCMD)res), out height);
                if ((width > 0) && (height > 0))
                {
                    camModel.Bmp = new WriteableBitmap(width, height, 0, 0, PixelFormats.Bgra32, null);
                    camModel.Buf = Marshal.AllocHGlobal(uvcham.TDIBWIDTHBYTES(width * 32) * height);
                    if (camModel.Cam.start(camModel.Buf,(nEvent)=>
                    {
                        if (camModel.Cam != null)
                        {
                            if ((nEvent & uvcham.eEVENT.IMAGE) != 0)
                                OnEventImage();
                            else if ((nEvent & uvcham.eEVENT.ERROR)!=0)
                                OnEventError();
                            else if ((nEvent & uvcham.eEVENT.DISCONNECT)!=0)
                                OnEventDisconnect();
                        }
                    }) < 0)
                    {
                    }
                    else
                    {
                        int nmin, nmax, ndef;
                        camModel.Cam.range(uvcham.eCMD.EXPOTIME, out nmin, out nmax, out ndef);
                        camModel.Cam.range(uvcham.eCMD.WBRED, out nmin, out nmax, out ndef);
                        camModel.Cam.range(uvcham.eCMD.WBGREEN, out nmin, out nmax, out ndef);
                        camModel.Cam.range(uvcham.eCMD.WBBLUE, out nmin, out nmax, out ndef);
                        UpdateExpoTime();
                        UpdateAwb();
                        int val = 0;
                        camModel.Cam.get(uvcham.eCMD.AEXPO, out val);
                        camModel.Cam.get(uvcham.eCMD.WBMODE, out val);

                        DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0,0,1)};
                        timer.Tick += (object sender, EventArgs e) =>
                        {
                            if (camModel.Cam != null)
                            {
                                UpdateExpoTime();
                                UpdateAwb() ;
                            }

                        };
                        timer.Start();
                    }
                }
            }
        }

        private void OnStart()
        {
            if (camModel.Cam != null)
                return;
            uvcham.Device[] arr = uvcham.Enum();
            if (arr.Length <= 0)
            {
            }
            else if (1 == arr.Length)
                startDevice(arr[0].id);

        }
        */
        public List<string> deletePicList  { get; set; }

        private CommandBase _photoCommand;

        public CommandBase PhotoCommand
        {
            get
            {
                if (_photoCommand == null)
                { 
                    _photoCommand = new CommandBase();
                    _photoCommand.DoExecute = new Action<object>(obj => { 

                    });
                }
                return _photoCommand;
            }
        }

        private CommandBase _checkCommand;

        public CommandBase CheckCommand
        {
            get
            {
                if (_checkCommand == null)
                {
                    _checkCommand = new CommandBase();
                    _checkCommand.DoExecute = new Action<object>(obj => {

                        CheckBox ck = obj as CheckBox;
                        if (ck != null)
                        {
                            if (ck.IsChecked == true)
                            {
                                if (deletePicList == null)
                                {
                                    deletePicList = new List<string>();
                                }
                                deletePicList.Add(ck.Name);
                            }
                            else
                            { 
                            }
                        }
                    });
                }
                return _checkCommand;
            }
        }
        public CameraViewModel()
        {
            camModel.VM_Model = new ObservableCollection<CameraModel.Model>();
            //for (int i = 0; i < 2; i++)
            //{
            //    camModel.VM_Model.Add(new Model() { Path = @"C:\Users\14068\Desktop\test2.jpg" });
            //}
            string spath = @"C:\Users\14068\Desktop\test2.jpg";
            int m = spath.LastIndexOf(@"\");
            int n =spath.LastIndexOf(".");
            camModel.VM_Model.Add(new Model() { Path = spath, Name= spath.Substring(m+1,n-m-1) });

            //camModel.VM_Model.RemoveAt(0);
        }










        private bool _isMyChecked;
        public bool IsMyChecked
        {
            get => _isMyChecked;
            set
            {
                if (_isMyChecked != value)
                {
                    _isMyChecked = value;
                    NotifyChanged();
                    HandleCheckChange();
                }
            }
        }

        public string ImgPath { get; set; }

        private List<string> _selectedImagePaths = new List<string>();
        public List<string> SelectedImagePaths
        {
            get => _selectedImagePaths;
            set
            {
                _selectedImagePaths = value;
                NotifyChanged();
            }
        }

        // 触发检查状态改变后的处理
        private void HandleCheckChange()
        {
            if (IsMyChecked)
            {
                if (!string.IsNullOrEmpty(ImgPath) && !_selectedImagePaths.Contains(ImgPath))
                {
                    _selectedImagePaths.Add(ImgPath);
                }
            }
            else
            {
                _selectedImagePaths.Remove(ImgPath);
            }
        }









    }

   
}
